package coml.jiyun.hp.day02;

import android.os.Bundle;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import coml.jiyun.hp.day02.adapter.VpMainAdapter;
import coml.jiyun.hp.day02.fragment.CollectionFragment;
import coml.jiyun.hp.day02.fragment.MainPageFragment;
import coml.jiyun.hp.day02.fragment.MeFragment;

public class MainActivity extends AppCompatActivity {

    /**
     * 首页
     */
    private TextView mToolTv;
    private Toolbar mMainTool;
    private ViewPager mMainVp;
    private TabLayout mMainTab;
    private ArrayList<String> mTitle;
    private ArrayList<Fragment> mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mToolTv = (TextView) findViewById(R.id.tool_tv);
        mMainTool = (Toolbar) findViewById(R.id.main_tool);
        mMainVp = (ViewPager) findViewById(R.id.main_vp);
        mMainTab = (TabLayout) findViewById(R.id.main_tab);

        mMainTool.setTitle("");
        setSupportActionBar(mMainTool);

        initTitle();
        initFragment();

        VpMainAdapter vpMainAdapter = new VpMainAdapter(getSupportFragmentManager(), mTitle, mFragment);
        mMainVp.setAdapter(vpMainAdapter);
        mMainTab.setupWithViewPager(mMainVp);

        //添加选择器
        for (int i = 0; i < mTitle.size(); i++) {
            TabLayout.Tab tab = mMainTab.getTabAt(i);
            if (i == 0) {
                tab.setIcon(R.drawable.se_main);
            }else if (i ==1 ){
                tab.setIcon(R.drawable.se_collection);
            }else {
                tab.setIcon(R.drawable.se_me);
            }
        }

        mMainTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab选中
                int position = tab.getPosition();
                mToolTv.setText(mTitle.get(position));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //取消选中
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //重复选中
            }
        });
    }

    private void initFragment() {
        mFragment = new ArrayList<>();

        mFragment.add(MainPageFragment.getInstance());
        mFragment.add(CollectionFragment.getInstance());
        mFragment.add(MeFragment.getInstance());
    }

    private void initTitle() {
        mTitle = new ArrayList<>();
        mTitle.add("首页");
        mTitle.add("收藏");
        mTitle.add("我的");
    }
}